﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Configurations;
using System.Globalization;

namespace Pixysoft.Framework.Logs.Core
{
    class LogRecord : ILogRecord, IComparable<LogRecord>
    {
        IXmlNode node;

        public LogRecord(IXmlNode node)
        {
            this.node = node;
        }


        public string Id
        {
            get { return node.Properties[StringHelper.default_attribute_id]; }
            set { node.Properties[StringHelper.default_attribute_id] = value; }
        }


        public DateTime Date
        {
            get
            {
                return node.Properties.Get<DateTime>(StringHelper.default_attribute_time, StringHelper.default_log_dateformat);
            }
        }


        public string RawDate
        {
            get { return node.Properties[StringHelper.default_attribute_time]; }
            set { node.Properties[StringHelper.default_attribute_time] = value; }
        }


        public LogLevel Level
        {
            get { return LogLevelCollection.GetLogLevel(node.Properties[StringHelper.default_attribute_level]); }
            set { node.Properties[StringHelper.default_attribute_level] = value.ToString(); }
        }


        public string Ip
        {
            get { return node.Properties[StringHelper.default_attribute_ip]; }
            set { node.Properties[StringHelper.default_attribute_ip] = value; }
        }


        public string Text
        {
            get { return node.Text; }
            set { node.Text = value; }
        }

        public string Module
        {
            get
            {
                return node.Properties[StringHelper.default_attribute_module];
            }
            set
            {
                node.Properties[StringHelper.default_attribute_module] = value;
            }
        }

        public string Filepath
        {
            get { return node.Properties[StringHelper.default_attribute_filepath]; }
            set
            {
                if (string.IsNullOrEmpty(value))
                    value = StringHelper.default_log_filepath;

                value = StringFilter.RegularPath(value);

                node.Properties[StringHelper.default_attribute_filepath] = value;
            }
        }

        public ILogFrameCollection Frames
        {
            get
            {
                return new LogFrameCollection(node.Nodes[StringHelper.default_element_frame]);
            }
            set
            {
                node.Nodes.Remove(StringHelper.default_element_frame);

                if (value == null)
                    return;

                foreach (ILogFrame frame in value)
                    this.Frames.Add(frame);
            }
        }

        public int CompareTo(LogRecord other)
        {
            if (this.Date > other.Date)
                return 1;

            else if (this.Date == other.Date)
                return 0;

            else
                return -1;
        }

        public int CompareTo(ILogRecord other)
        {
            if (this.Date > other.Date)
                return 1;

            else if (this.Date == other.Date)
                return 0;

            else
                return -1;
        }



    }
}
